package cn.jdy.plugins.admin;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.safe.SafeUtil;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Member;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理
 *
 * @author zhaigt
 */
@Controller
@RequestMapping("/member")
public class MemberCtrl extends BaseCtrl {

	// 首页显示新通知数量
	@RequestMapping("/queryIndexInfo")
	@ResponseBody
	public Map<String, Object> queryIndexInfo() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 后台首页查询未审核人员数量
		jsonMap.put("count", memberService.count("userType","1","status","1"));
		return jsonMap;
	}

	/**
	 * 初始化
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String userType) {
		return new ModelAndView("1".equals(userType)?"/member/corpList.jsp":"/member/personList.jsp");
	}

	/**
	 * 查询
	 *
	 * @param member
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Member member, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", memberService.query(member, fromDate, toDate));
			Long count = memberService.count(member, fromDate, toDate);
			jsonMap.put("iTotalDisplayRecords", count);
			jsonMap.put("iTotalRecords", count);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询错误", e);
		}
		return jsonMap;
	}

	@RequestMapping("/add")
	public ModelAndView add(String userType) {
		return new ModelAndView("1".equals(userType)?"/member/corpEdit.jsp":"/member/personEdit.jsp");
	}
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		ModelAndView mv = new ModelAndView();
		Member member = this.memberService.find(id);
		if(member != null) {
			mv.setViewName("1".equals(member.getUserType())?"/member/corpEdit.jsp":"/member/personEdit.jsp");
			mv.addObject("data", member);
		}
		return mv;
	}

	@RequestMapping("/oper")
	@ResponseBody
	public Map<String, Object> oper(String id, int type, String value) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String result = this.memberService.updateBatch(id, type, value);
			jsonMap.put("statusCode", 200);
			if(result != null) jsonMap.put("info", result);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "会员批量更新异常！", e);
		}
		return jsonMap;
	}

	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Member entity) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(entity.getId() == null || entity.getId().length()==0) {
				entity.setId(null);
				entity.setPhoto("/images/no-img_mid_.jpg");
				entity.setCreateTime(DateUtil.getCurrentTime());
				entity.setPassword(SafeUtil.md5(entity.getUserName()).substring(8, 16));
				this.memberService.save(entity);
			}else
				this.memberService.updateWithValidator(entity);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "会员批量更新异常！", e);
		}
		return jsonMap;
	}

	/**
	 * 下载Excel
	 *
	 * @return
	 */
	@RequestMapping("/downExcel")
	public void downExcel(Member member, HttpServletResponse response) {
		List<Member> list = this.memberService.query(member);
		String[] titles, fieldNames;
		String name;
		if("0".equals(member.getUserType())) {
			name = "个人会员";
			titles = new String[]{"用户名", "手机号", "状态", "认证", "注册时间", "余额"};
			fieldNames = new String[]{"userName", "mobile", "status", "approve", "createTime", "money"};
		} else {
			name = "企业会员";
			titles = new String[]{"用户名", "状态", "认证", "企业名称", "注册时间", "余额"};
			fieldNames = new String[]{"userName", "status", "approve", "company.companyName", "createTime", "money"};
		}
		super.downExcel(list, titles, fieldNames, name, response);
	}
}